using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System;
	using System.Web.Mvc;
	using System.Web.Security;
	using System.Collections.Generic;
	using WebClient.Models.ViewModels;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using WebClient.Models;
	using System.IO;
	using System.Web;
    using WebClient.CF_DAL_WS;
    using System.Drawing;
	#endregion
	public abstract class BaseController : Controller
	{
		protected FoodTypeRepository _repFoodType = new FoodTypeRepository();
		protected MenuRepository _repMenu = new MenuRepository();
		protected InformationWebRepository _repInformationWeb = new InformationWebRepository();
		protected RestaurantRepository _repRestaurant = new RestaurantRepository();
		protected ContactRepository _repContact = new ContactRepository();
		protected ReplyRepository _repReply = new ReplyRepository();
		protected FoodRepository _repFood = new FoodRepository();
        protected FoodDetailRepository _repFoodDetail = new FoodDetailRepository();
		protected CityRepository _repCity = new CityRepository();
		protected OrdersRepository _repOrders = new OrdersRepository();
		protected DistrictRepository _repDistrict = new DistrictRepository();
		protected OrderDetailRepository _repOrderDetail = new OrderDetailRepository();
		protected UsersRepository _repUsers = new UsersRepository();
        protected CartRepository _repCart = new CartRepository();

		protected string RenderPartialViewToString()
		{
			return RenderPartialViewToString(null, null);
		}

		protected string RenderPartialViewToString(string viewName)
		{
			return RenderPartialViewToString(viewName, null);
		}

		protected string RenderPartialViewToString(string viewName, object model)
		{
			if (string.IsNullOrEmpty(viewName))
				viewName = ControllerContext.RouteData.GetRequiredString("action");
			ViewData.Model = model;
			using (StringWriter sw = new StringWriter())
			{
				ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
				ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
				viewResult.View.Render(viewContext, sw);
				return sw.GetStringBuilder().ToString();
			}
		}

        protected bool AuthenticateSession(ref string username)
        {
            HttpCookie SlmSessionId = new HttpCookie("SLMFSessionId");
            SlmSessionId = HttpContext.Request.Cookies["SLMFSessionId"];
            if (SlmSessionId != null && SlmSessionId.Value != "" &&
                 SlmSessionId.Value != null)
            {
                string strSessionId = SlmSessionId.Value.ToString();
                var errtxt = string.Empty;
                username = LookCookie();
                if ((errtxt = DALService.Instance.Validation_ValidateUser(username, strSessionId)) == string.Empty)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        protected string LookCookie()
        {
            HttpCookie SlmMemberCookie = new HttpCookie("SLMFMembers");
            SlmMemberCookie = HttpContext.Request.Cookies["SLMFMembers"];
            string strUser = "";
            if (SlmMemberCookie != null && SlmMemberCookie.Value != "" &&
                 SlmMemberCookie.Value != null)
            {
                strUser = SlmMemberCookie.Value.ToString();
            }
            return strUser;
        }

        protected string LookRole()
        {
            HttpCookie SlmMemberCookie = new HttpCookie("SLMFRole");
            SlmMemberCookie = HttpContext.Request.Cookies["SLMFRole"];
            string strUser = "";
            if (SlmMemberCookie != null && SlmMemberCookie.Value != "" &&
                 SlmMemberCookie.Value != null)
            {
                strUser = SlmMemberCookie.Value.ToString();
            }
            return strUser;
        }

		public GroupViewModel CreateViewModel(DataTransferViewModel dataTransfer)
		{
			GroupViewModel viewModel = new GroupViewModel();
            if (dataTransfer.EnumViewModelType.ToString().Contains("ADMIN") && LookRole().ToUpper() == "ADMIN")
            {
                viewModel.CityModel = RetrieveCityViewModel(dataTransfer);
                viewModel.FoodTypeModel = RetrieveFoodTypeViewModel(dataTransfer);
            }
            else if (dataTransfer.EnumViewModelType.ToString().Contains("HOME"))
            {
                // Show on foodter
                viewModel.AllDistrictModel = RetrieveAllDistrictViewModel(dataTransfer);
                viewModel.AllRestaurantModel = RetrieveAllRestaurantViewModel(dataTransfer);
                viewModel.FoodTypeModel = RetrieveFoodTypeViewModel(dataTransfer);
                var username = string.Empty;
                if ((username = LookCookie()) != string.Empty)
                {
                    viewModel.UsersModel = RetrieveUsersByUserNameViewModel(username);
                }
            }
			switch (dataTransfer.EnumViewModelType)
			{
				case EnumViewModel.ADMIN_DETAILOF_RESTAURANT:
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_FOOD:
                    viewModel.FoodModel = RetrieveFoodViewModel(dataTransfer);
                    viewModel.FoodDetailModel = RetrieveFoodDetailViewModel(dataTransfer);
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_USERS:
                    viewModel.UsersModel = RetrieveDetailOfUsersViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_FOODTYPE:
					break;
				case EnumViewModel.ADMIN_MENU:
                    viewModel.MenuModel = RetrieveMenuViewModel(dataTransfer);
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_INFORMATIONWEB:
                    viewModel.InformationWebModel = RetrieveInformationWebViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_RESTAURANT:
                    viewModel.RestaurantModel = RetrieveRestaurantViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_CONTACT:
                    viewModel.ContactModel = RetrieveContactViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_REPLY:
                    viewModel.ReplyModel = RetrieveReplyViewModel(dataTransfer);
                    viewModel.ContactModel = RetrieveDetailOfContactViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_FOOD:
                    viewModel.FoodModel = RetrieveFoodViewModel(dataTransfer);
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_USERS:
                    viewModel.UsersModel = RetrieveUsersViewModel(dataTransfer);
					break;
                case EnumViewModel.ADMIN_MANAGER:
                    viewModel.UsersModel = RetrieveManagerViewModel(dataTransfer);
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
                    break;
				case EnumViewModel.ADMIN_CITY:
					break;
				case EnumViewModel.ADMIN_ORDERS:
                    viewModel.OrdersModel = RetrieveOrdersViewModel(dataTransfer);
                    if (dataTransfer.IDRestaurant != -1)
                    {
                        viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
                    }
					break;
                case EnumViewModel.ADMIN_ORDERS_REVENUE:
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_ORDERS_REVENUE_EXECUTE:
                    viewModel.OrdersModel = RetrieveOrdersRevenueViewModel(dataTransfer);
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
                    break;
				case EnumViewModel.ADMIN_DISTRICT:
                    viewModel.DistrictModel = RetrieveDistrictViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_ORDERDETAIL:
                    viewModel.OrdersModel = RetrieveOrdersViewModel(dataTransfer);
                    viewModel.SelectedOrdersModel = RetrieveDetailOfOrdersViewModel(dataTransfer);
                    viewModel.OrderDetailModel = RetrieveOrderDetailViewModel(dataTransfer);
                    if (dataTransfer.IDRestaurant != -1)
                    {
                        viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
                    }
					break;
				case EnumViewModel.HOME_REGISTER:
					break;
                case EnumViewModel.HOME_INDEX:
                    viewModel.AllCityModel = RetrieveCityViewModel(dataTransfer);
                    viewModel.DistrictModelByCity = RetrieveDistrictOnlyByCityViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_RESTAURANT:
                    viewModel.AllCityModel = RetrieveCityViewModel(dataTransfer);
                    viewModel.DistrictModelByCity = RetrieveDistrictOnlyByCityViewModel(dataTransfer);
                    viewModel.RestaurantModel = RetrieveRestaurantViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_ORDER:
                    viewModel.RestaurantModel = RetrieveDetailOfRestaurantViewModel(dataTransfer);
                    viewModel.AllCityModel = RetrieveCityViewModel(dataTransfer);
                    viewModel.AllFoodModel = RetrieveFoodViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CUISINES:
                    viewModel.AllCityModel = RetrieveCityViewModel(dataTransfer);
                    viewModel.DistrictModelByCity = RetrieveDistrictOnlyByCityViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_DETAILOF_USERS:
                    viewModel.UsersModel = RetrieveDetailOfUsersViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_ORDERHISTORY:
                    viewModel.OrdersModel = RetrieveOrdersViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_ORDERHISTORY_ORDERDETAIL:
                    viewModel.OrdersModel = RetrieveOrdersViewModel(dataTransfer);
                    viewModel.SelectedOrdersModel = RetrieveDetailOfOrdersViewModel(dataTransfer);
                    viewModel.OrderDetailModel = RetrieveOrderDetailViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_DETAIOF_FOOD:
                    viewModel.FoodDetailModel = RetrieveFoodDetailViewModel(dataTransfer);
                    break;
			}
			return viewModel;
		}

        public bool UploadFile(HttpPostedFileBase file, string name, ref byte[] buffer, ref string refName)
		{
			try
			{
                FileInfo f = new FileInfo(file.FileName);
                long ngay = DateTime.Now.ToBinary();
                refName = name + ngay + f.Extension;
                if (file == null)
                    return false;

                buffer = new byte[file.ContentLength];
                file.InputStream.Read(buffer, 0, file.ContentLength);
                return true;
			}
			catch
			{
				return false;
			}
		}

		public void DeleteFile(string filePath)
		{
			FileInfo file = new FileInfo(filePath);
			if (file.Exists)
			{
				file.Delete();
			}
		}

		public bool SendingMail(string subject, string emailContent, string to)
		{
			bool result = false;
			try
			{
				System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient();
				System.Net.NetworkCredential auth = new System.Net.NetworkCredential(WebConfiguration.EmailLienHe, WebConfiguration.PasswordEmailLienHe);
				mailclient.Host = WebConfiguration.MailServer;
				mailclient.EnableSsl = true;
				mailclient.UseDefaultCredentials = true;
				mailclient.Credentials = auth;
				System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(WebConfiguration.EmailLienHe, to);
				mm.Subject = subject;
				mm.IsBodyHtml = true;
				mm.Body = emailContent;
				mailclient.Send(mm);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public FoodTypeViewModel RetrieveFoodTypeViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repFoodType.SelectAll();
			return new FoodTypeViewModel
			{
				GetModel = new GetFoodTypeViewModel
				{
                    LstObjModel = lst,
                    TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddFoodTypeViewModel
				{
				},
				EditModel = new EditFoodTypeViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public MenuViewModel RetrieveMenuViewModel(DataTransferViewModel dataTransfer)
		{
			List<MenuDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDRestaurant != -1)
			{
				lst = _repMenu.SelectByIDRestaurant(dataTransfer.IDRestaurant);
			}
			else
			{
                lst = _repMenu.SelectAll();
			}
            totalitem = lst.Count;

			return new MenuViewModel
			{
				GetModel = new GetMenuViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddMenuViewModel
				{
				},
				EditModel = new EditMenuViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new MenuReferKeys()
				{
					IDRestaurant = dataTransfer.IDRestaurant,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public InformationWebViewModel RetrieveInformationWebViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repInformationWeb.SelectAll();
			return new InformationWebViewModel
			{
				GetModel = new GetInformationWebViewModel
				{
                    LstObjModel = lst,
                    TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddInformationWebViewModel
				{
				},
				EditModel = new EditInformationWebViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public RestaurantViewModel RetrieveRestaurantViewModel(DataTransferViewModel dataTransfer)
		{
			List<RestaurantDTO> lst = null;
			int totalitem = -1;
            if (dataTransfer.IDDistrict != -1 && dataTransfer.IDFoodType != -1)
            {
                lst = _repRestaurant.SelectByIDDistrictAndIDFoodType(dataTransfer.IDDistrict, dataTransfer.IDFoodType);
            }
			else if (dataTransfer.IDDistrict != -1)
			{
				lst = _repRestaurant.SelectByIDDistrict(dataTransfer.IDDistrict);
			}
            else if (dataTransfer.IDFoodType != -1)
			{
				lst = _repRestaurant.SelectByIDFoodType(dataTransfer.IDFoodType);
			}
			else
			{
                lst = _repRestaurant.SelectAll();
			}
            totalitem = lst.Count;

			return new RestaurantViewModel
			{
				GetModel = new GetRestaurantViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddRestaurantViewModel
				{
				},
				EditModel = new EditRestaurantViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new RestaurantReferKeys()
				{
					IDDistrict = dataTransfer.IDDistrict,
					IDFoodType = dataTransfer.IDFoodType,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public RestaurantViewModel RetrieveAllRestaurantViewModel(DataTransferViewModel dataTransfer)
        {
            List<RestaurantDTO> lst = _repRestaurant.SelectAll();
            var totalitem = lst.Count;

            return new RestaurantViewModel
            {
                GetModel = new GetRestaurantViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddRestaurantViewModel
                {
                },
                EditModel = new EditRestaurantViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new RestaurantReferKeys()
                {
                    IDDistrict = dataTransfer.IDDistrict,
                    IDFoodType = dataTransfer.IDFoodType,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public ContactViewModel RetrieveContactViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repContact.SelectAll();
			return new ContactViewModel
			{
				GetModel = new GetContactViewModel
				{
					LstObjModel = lst,
					TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddContactViewModel
				{
				},
				EditModel = new EditContactViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public ContactViewModel RetrieveDetailOfContactViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repContact.RetrieveByID(dataTransfer.IDContact);
            return new ContactViewModel
            {
                GetModel = new GetContactViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddContactViewModel
                {
                },
                EditModel = new EditContactViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public ReplyViewModel RetrieveReplyViewModel(DataTransferViewModel dataTransfer)
		{
            List<ReplyDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDContact != -1)
			{
				lst = _repReply.SelectByIDContact(dataTransfer.IDContact);
			}
			else
			{
                lst = _repReply.SelectAll();
			}
            totalitem = lst.Count;

			return new ReplyViewModel
			{
				GetModel = new GetReplyViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddReplyViewModel
				{
				},
				EditModel = new EditReplyViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new ReplyReferKeys()
				{
					IDContact = dataTransfer.IDContact,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public FoodViewModel RetrieveFoodViewModel(DataTransferViewModel dataTransfer)
		{
            List<FoodDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDMenu != -1)
			{
				lst = _repFood.SelectByIDMenu(dataTransfer.IDMenu);
			}
            else if (dataTransfer.IDFood != -1)
            {
                lst = _repFood.RetrieveByID(dataTransfer.IDFood);
            }
            else
            {
                lst = _repFood.SelectAll();
            }
            totalitem = lst.Count;

			return new FoodViewModel
			{
				GetModel = new GetFoodViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddFoodViewModel
				{
				},
				EditModel = new EditFoodViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new FoodReferKeys()
				{
					IDMenu = dataTransfer.IDMenu,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public FoodDetailViewModel RetrieveFoodDetailViewModel(DataTransferViewModel dataTransfer)
        {
            List<FoodDetailDTO> lst = _repFoodDetail.RetrieveByID(dataTransfer.IDFood);
            int totalitem = lst.Count;

            return new FoodDetailViewModel
            {
                GetModel = new GetFoodDetailViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddFoodDetailViewModel
                {
                },
                EditModel = new EditFoodDetailViewModel
                {
                },
                ReferKeys = new FoodDetailReferKeys()
                {
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public CityViewModel RetrieveCityViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repCity.SelectAll();
			return new CityViewModel
			{
				GetModel = new GetCityViewModel
				{
                    LstObjModel = lst,
                    TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddCityViewModel
				{
				},
				EditModel = new EditCityViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public OrdersViewModel RetrieveOrdersViewModel(DataTransferViewModel dataTransfer)
		{
            List<OrdersDTO> lst = null;
			int totalitem = -1;
            if (dataTransfer.IDRestaurant != -1)
            {
                lst = _repOrders.SelectByIDRestaurant(dataTransfer.IDRestaurant);
                totalitem = lst.Count;
            }
			else if (dataTransfer.IDUsers != Guid.Parse("11112222-1111-1111-1111-111122223333"))
			{
				lst = _repOrders.SelectByIDUsers(dataTransfer.IDUsers);
                totalitem = lst.Count;
			}
			else
			{
                lst = _repOrders.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumOrdersPerPage);
                totalitem = _repOrders.GetTotalItem();
			}
            
			return new OrdersViewModel
			{
				GetModel = new GetOrdersViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddOrdersViewModel
				{
				},
				EditModel = new EditOrdersViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new OrdersReferKeys()
				{
					IDUsers = dataTransfer.IDUsers,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public OrdersViewModel RetrieveOrdersRevenueViewModel(DataTransferViewModel dataTransfer)
        {
            string[] supportedFormats = new String[] { "MM/dd/yyyy HH:mm:ss" };
            var lst = new List<OrdersDTO>();
            foreach (var item in _repOrders.SelectAll())
            {
                var orderDate = DateTime.ParseExact(item.OrderDate, supportedFormats,
                                                    System.Globalization.CultureInfo.CurrentCulture,
                                                    System.Globalization.DateTimeStyles.None);
                if (item.IsShipped && dataTransfer.From <= orderDate && orderDate <= dataTransfer.To)
                {
                    lst.Add(item);
                }
            }
            var totalitem = lst.Count;

            return new OrdersViewModel
            {
                GetModel = new GetOrdersViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddOrdersViewModel
                {
                },
                EditModel = new EditOrdersViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new OrdersReferKeys()
                {
                    IDUsers = dataTransfer.IDUsers,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public DistrictViewModel RetrieveDistrictViewModel(DataTransferViewModel dataTransfer)
		{
            List<DistrictDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDCity != -1)
			{
				lst = _repDistrict.SelectByIDCity(dataTransfer.IDCity);
			}
			else
			{
				lst = _repDistrict.SelectAll();
			}
            totalitem = lst.Count;

			return new DistrictViewModel
			{
				GetModel = new GetDistrictViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddDistrictViewModel
				{
				},
				EditModel = new EditDistrictViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new DistrictReferKeys()
				{
					IDCity = dataTransfer.IDCity,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public OrderDetailViewModel RetrieveOrderDetailViewModel(DataTransferViewModel dataTransfer)
		{
            List<OrderDetailDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDOrder != -1)
			{
				lst = _repOrderDetail.SelectByIDOrder(dataTransfer.IDOrder);
			}
            else if (dataTransfer.IDFood != -1)
			{
				lst = _repOrderDetail.SelectByIDFood(dataTransfer.IDFood);
			}
			else
			{
                lst = _repOrderDetail.SelectAll();
			}
            totalitem = lst.Count;

			return new OrderDetailViewModel
			{
				GetModel = new GetOrderDetailViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddOrderDetailViewModel
				{
				},
				EditModel = new EditOrderDetailViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new OrderDetailReferKeys()
				{
					IDOrder = dataTransfer.IDOrder,
					IDFood = dataTransfer.IDFood,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public UsersViewModel RetrieveUsersViewModel(DataTransferViewModel dataTransfer)
		{
			return new UsersViewModel
			{
				GetModel = new GetUsersViewModel
				{
					LstObjModel = _repUsers.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumUsersPerPage),
					TotalItem = _repUsers.GetTotalItem(dataTransfer.Role),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddUsersViewModel
				{
				},
				EditModel = new EditUsersViewModel
				{
					ID = dataTransfer.GuidID,
				}
			};
		}

        public UsersViewModel RetrieveManagerViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repUsers.SelectManager(dataTransfer.IDRestaurant);
            return new UsersViewModel
            {
                GetModel = new GetUsersViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddUsersViewModel
                {
                },
                EditModel = new EditUsersViewModel
                {
                    ID = dataTransfer.GuidID,
                }
            };
        }

		public RestaurantViewModel RetrieveDetailOfRestaurantViewModel(DataTransferViewModel dataTransfer)
		{
			return new RestaurantViewModel
			{
				GetModel = new GetRestaurantViewModel
				{
					LstObjModel = _repRestaurant.RetrieveByID(dataTransfer.IDRestaurant)
				},
				EditModel = new EditRestaurantViewModel
				{
                    ID = dataTransfer.IDRestaurant,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public FoodViewModel RetrieveDetailOfFoodViewModel(DataTransferViewModel dataTransfer)
		{
			return new FoodViewModel
			{
				GetModel = new GetFoodViewModel
				{
					LstObjModel = _repFood.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditFoodViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public UsersViewModel RetrieveDetailOfUsersViewModel(DataTransferViewModel dataTransfer)
		{
			return new UsersViewModel
			{
				GetModel = new GetUsersViewModel
				{
					LstObjModel = _repUsers.RetrieveByID(dataTransfer.GuidID)
				},
				EditModel = new EditUsersViewModel
				{
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public UsersViewModel RetrieveUsersByUserNameViewModel(string username)
        {
            var model = new UsersViewModel
            {
                GetModel = new GetUsersViewModel
                {
                    LstObjModel = new List<UsersDTO>()
                },
                EditModel = new EditUsersViewModel{
                }
            };
            model.GetModel.LstObjModel.Add(_repUsers.SelectByUserName(username));
            return model;
        }

        public OrdersViewModel RetrieveDetailOfOrdersViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repOrders.RetrieveByID(dataTransfer.IDOrder);
            var totalitem = lst.Count;

            return new OrdersViewModel
            {
                GetModel = new GetOrdersViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddOrdersViewModel
                {
                },
                EditModel = new EditOrdersViewModel
                {
                },
                ReferKeys = new OrdersReferKeys()
                {
                }
            };
        }

        /////////////////////////////////////////// HOME ////////////////////////////////////////
        public DistrictViewModel RetrieveDistrictOnlyByCityViewModel(DataTransferViewModel dataTransfer)
        {
            List<DistrictDTO> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDCity != -1)
            {
                lst = _repDistrict.SelectByIDCity(dataTransfer.IDCity);
            }
            else
            {
                lst = new List<DistrictDTO>();
            }
            totalitem = lst.Count;

            return new DistrictViewModel
            {
                GetModel = new GetDistrictViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDistrictViewModel
                {
                },
                EditModel = new EditDistrictViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DistrictReferKeys()
                {
                    IDCity = dataTransfer.IDCity,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DistrictViewModel RetrieveAllDistrictViewModel(DataTransferViewModel dataTransfer)
        {
            List<DistrictDTO> lst = _repDistrict.SelectAll();
            int totalitem = lst.Count;

            return new DistrictViewModel
            {
                GetModel = new GetDistrictViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDistrictViewModel
                {
                },
                EditModel = new EditDistrictViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DistrictReferKeys()
                {
                    IDCity = dataTransfer.IDCity,
                },
                InfoText = dataTransfer.InfoText
            };
        }
	}
}